package com.ysd.lis.service.bac.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.SysRuleSet;
import com.ysd.lis.entity.bac.BacCultureCombination;
import com.ysd.lis.entity.bac.BacReqPurpose;
import com.ysd.lis.entity.bac.BacReqPurposeDetail;
import com.ysd.lis.entity.bac.BacWorkFlow;
import com.ysd.lis.entity.lab.LabRptGroup;
import com.ysd.lis.entity.lab.LabSampleType;
import com.ysd.lis.mapper.bac.BacReqPurposeDetailMapper;
import com.ysd.lis.mapper.bac.BacReqPurposeMapper;
import com.ysd.lis.request.bac.PurposeDto;
import com.ysd.lis.request.bac.PurposeParam;
import com.ysd.lis.service.bac.BacReqPurposeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author prs
 * @since 2024-04-10
 */
@Service
public class BacReqPurposeServiceImpl extends ServiceImpl<BacReqPurposeMapper, BacReqPurpose> implements BacReqPurposeService {

    @Autowired
    BacReqPurposeMapper bacReqPurposeMapper;
    @Autowired
    BacReqPurposeDetailMapper bacReqPurposeDetailMapper;

    @Override
    public Result findPurposeList(PurposeParam param) {
        MPJLambdaWrapper<BacReqPurpose> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(BacReqPurpose.class);
        queryWrapper.selectAs("sa", LabSampleType::getSampleTypeName, BacReqPurpose::getSampleTypeName);
        queryWrapper.selectAs("wo", BacWorkFlow::getWorkFlowName, BacReqPurpose::getWorkFlowName);
        queryWrapper.selectAs("cu", BacCultureCombination::getName, BacReqPurpose::getCultureCombinationName);
        queryWrapper.selectAs("ru", SysRuleSet::getRuleName, BacReqPurpose::getSampleNoRuleName);
        queryWrapper.selectAs("rp", LabRptGroup::getRptGroupName, BacReqPurpose::getRptGroupName);
        queryWrapper.leftJoin(LabSampleType.class, "sa", p -> p.eq(LabSampleType::getSampleTypeCode, BacReqPurpose::getSampleTypeCode).eq(LabSampleType::getDelFlag, 0));
        queryWrapper.leftJoin(BacWorkFlow.class, "wo", p -> p.eq(BacWorkFlow::getWorkFlowCode, BacReqPurpose::getFlowNo).eq(BacWorkFlow::getDelFlag, 0));
        queryWrapper.leftJoin(BacCultureCombination.class, "cu", p -> p.eq(BacCultureCombination::getCode, BacReqPurpose::getCulComninaNo).eq(BacCultureCombination::getDelFlag, 0));
        queryWrapper.leftJoin(SysRuleSet.class, "ru", SysRuleSet::getId, BacReqPurpose::getSampleNoRule);
        queryWrapper.leftJoin(LabRptGroup.class, "rp", p -> p.eq(LabRptGroup::getRptGroup, BacReqPurpose::getRptGroup).eq(LabRptGroup::getDelFlag, 0));
        queryWrapper.eq(BacReqPurpose::getDelFlag, 0);

        if (StringUtils.isNotBlank(param.getSeachValue())) {
            queryWrapper.and(wrapper -> wrapper.like(BacReqPurpose::getPurposeNo, param.getSeachValue()).or().like(BacReqPurpose::getPurposeName, param.getSeachValue()).or().like(BacReqPurpose::getInputCode, param.getSeachValue()));
        }
        if (StringUtils.isNotBlank(param.getRptGroup())) {
            queryWrapper.eq(BacReqPurpose::getRptGroup, param.getRptGroup());
        }
        queryWrapper.orderByAsc(BacReqPurpose::getSeq).orderByAsc(BacReqPurpose::getCreateTime);
        List<BacReqPurpose> purposeList = bacReqPurposeMapper.selectList(queryWrapper);
        return Result.succ(1, "查询成功", purposeList);

    }

    @Override
    public Result addPurpose(BacReqPurpose bacReqPurpose) {
        int insert = bacReqPurposeMapper.insert(bacReqPurpose);
        if (insert > 0) {
            return Result.succ(1, "添加成功", bacReqPurpose);
        } else {
            return Result.fail(400, "添加失败", "");
        }
    }

    @Override
    public Result editPurpose(BacReqPurpose bacReqPurpose) {
        int updateById = bacReqPurposeMapper.updateById(bacReqPurpose);
        if (updateById > 0) {
            return Result.succ(1, "提交成功", bacReqPurpose);
        } else {
            return Result.fail(400, "修改失败", "");
        }
    }

    @Override
    public Result deletePurpose(BacReqPurpose bacReqPurpose) {
        int deleteById = bacReqPurposeMapper.deleteById(bacReqPurpose.getId());
        if (deleteById > 0) {
            return Result.succ(1, "删除成功", "");
        } else {
            return Result.fail(400, "删除失败", "");
        }
    }

    @Override
    public Result batchDeletePurposeList(PurposeDto dto) {
        List<BacReqPurpose> purposeList = dto.getPurposeList();
        List<String> ids = purposeList.stream().map(BacReqPurpose::getId).collect(Collectors.toList());
        List<String> Nos = purposeList.stream().map(BacReqPurpose::getPurposeNo).collect(Collectors.toList());
        //查询供应商下是否有评价登记
        LambdaQueryWrapper<BacReqPurposeDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BacReqPurposeDetail::getDelFlag, 0);
        queryWrapper.in(BacReqPurposeDetail::getPurposeNo, Nos);
        Integer cnt = bacReqPurposeDetailMapper.selectCount(queryWrapper);
        if (cnt > 0) {
            return Result.fail(400, "微生物检验目的下有标本类型，不能删除", null);
        }
        boolean b = this.removeByIds(ids);
        if (b) {
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail(400, "删除失败", "");
        }
    }
}
